package com.example.yjh.adcdemo;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cbtService.AndroidSDK.ADCService;
import com.cbtService.AndroidSDK.BeepService;
import com.cbtService.AndroidSDK.LedService;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button button;
    private TextView textView;
    private ProgressBar progressBar;
    Handler mHandler;
    LedService led_src;
    ImageView iv1,iv2;

    public static boolean iflag1=true;
    public static boolean iflag2=false;
    float i;
    int a=50;
    private final static  int pro_msgtype_1=101;
    private final static  int MSG_FINISH=1;
    Runnable ui_runnable =new Runnable() {
        @Override
        public void run() {
            Message msg=new Message();
            msg.what=pro_msgtype_1;
            msg.arg1=MSG_FINISH;
            msg.obj= ADCService._adc_on();
            mHandler.sendMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ADCService._init();
        BeepService._init();
        createHandler();
        led_src=new LedService();
        button=(Button)findViewById(R.id.button);
        textView=(TextView)findViewById(R.id.textView);
        progressBar=(ProgressBar)findViewById(R.id.progressBar);
        button.setOnClickListener(this);
        iv1=(ImageView)findViewById(R.id.imageView);
        iv2=(ImageView)findViewById(R.id.imageView2);
        iv1.setOnClickListener(this);
        iv2.setOnClickListener(this);


    }

    private void createHandler() {
        mHandler=new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if (msg.what==pro_msgtype_1){
                    switch (msg.arg1){
                        case MSG_FINISH:
                            i= (float) (((3.3* ADCService._adc_on())/4096));
                            textView.setText("ADC: "+i+"V");
                            if(i<1){
                                BeepService.PWMPlay(a);
                                iv2.setImageResource(R.drawable.laba);
                                iflag2=true;
                            }
                            else{
                                BeepService.PWMStop();
                                iv2.setImageResource(R.drawable.labaoff);
                                iflag2=false;
                            }
                            if(i>2.5){
                                led_src._setLedState(0,1);
                                iv1.setImageResource(R.drawable.ledon);
                                iflag1=false;
                            }
                            else{
                                led_src._setLedState(0,0);
                                iv1.setImageResource(R.drawable.ledoff);
                                iflag1=true;
                            }
                            float b=i*10;
                            progressBar.setProgress((int)b);
                            mHandler.postDelayed(ui_runnable,3000);
                            break;
                        default:
                            break;
                    }
                }
            }
        };

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.imageView:{
                if(iflag1){
                    led_src._setLedState(0,1);
                    iv1.setImageResource(R.drawable.ledon);
                    iflag1=false;
                }
                else {
                    led_src._setLedState(0,0);
                    iv1.setImageResource(R.drawable.ledoff);
                    iflag1=true;
                }
                break;
            }
            case R.id.button: {
                mHandler.post(ui_runnable);
            }
            case R.id.imageView2:{
                if(iflag2){
                    BeepService.PWMStop();
                    iv2.setImageResource(R.drawable.labaoff);
                    iflag2=false;
                }
                else {
                    BeepService.PWMPlay(a);
                    iv2.setImageResource(R.drawable.laba);
                    iflag2=true;
                }
                break;
            }
        }
    }
}
